package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.SoulLock;
import com.tanhua.dubbo.api.SoulLockApi;
import com.tanhua.dubbo.mapper.SoulLockMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SoulLockApiImpl implements SoulLockApi {
    @Autowired
    private SoulLockMapper soulLockMapper;

    @Override
    public Integer findIsLock(Long userId, String level) {
        QueryWrapper<SoulLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",level).eq("user_id",userId);
        Integer isLock = soulLockMapper.selectOne(queryWrapper).getIsLock();
        return isLock;
    }

    @Override
    public Long findReportId(Long userId, String level) {
        QueryWrapper<SoulLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",level).eq("user_id",userId);
        Long reportId = soulLockMapper.selectOne(queryWrapper).getReportId();
        return reportId;
    }

    @Override
    public void saveReportId(Long userId, String level, Long reportId) {
        QueryWrapper<SoulLock> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId).eq("level",level);

        SoulLock lock = new SoulLock();
        lock.setUserId(userId);
        lock.setIsLock(0);
        lock.setLevel(level);
        lock.setReportId(reportId);

        soulLockMapper.update(lock,queryWrapper);
    }


    @Override
    public void updateIsLock(Long userId, String level) {
        String level2;
        if ("初级".equals(level)) {
            level2 = "中级";
        }else if("中级".equals(level)) {
            level2 = "高级";
        }else {
            return;
        }

        QueryWrapper<SoulLock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("level",level2);

        SoulLock soulLock = new SoulLock();
        soulLock.setUserId(userId);
        soulLock.setIsLock(0);
        soulLock.setLevel(level2);

        soulLockMapper.update(soulLock,queryWrapper);


    }

}
